package com.samehope.ar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.samehope.ar.constant.DelFlagEnum;
import com.samehope.ar.constant.RoleNameEnum;
import com.samehope.ar.dto.ProjectAddParam;
import com.samehope.ar.dto.ProjectUpdateParam;
import com.samehope.ar.entity.Project;
import com.samehope.ar.entity.ProjectTeamMember;
import com.samehope.ar.entity.SysRole;
import com.samehope.ar.entity.SysUserRole;
import com.samehope.ar.mapper.ProjectMapper;
import com.samehope.ar.service.IProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.samehope.ar.service.IProjectTeamMemberService;
import com.samehope.ar.service.ISysRoleService;
import com.samehope.ar.service.ISysUserRoleService;
import com.samehope.ar.util.SessionUtil;
import com.samehope.ar.vo.UserProjectDataVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 项目 服务实现类
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysUserRoleService userRoleService;

    @Autowired
    private IProjectTeamMemberService teamMemberService;

    /**
     * 增加项目
     * @param param
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ProjectAddParam param) {

        final LocalDateTime now = LocalDateTime.now();

        Long companyId = SessionUtil.getCompanyId();

        Project project = this.baseMapper.findByCompanyAndProjectName(companyId, param.getProjectName(), null);
        if (project != null) {
            throw new IllegalArgumentException("项目名称已存在");
        }

        // 创建项目
        project = new Project();
        project.setAddress(param.getAddress());
        project.setCompanyId(SessionUtil.getCompanyId());
        project.setProjectDesc(param.getProjectDesc());
        project.setPicUrl(param.getPicUrl());
        project.setProjectName(param.getProjectName());
        project.setCoordinate(param.getCoordinate());
        project.setRemarks(param.getRemarks());
        project.setCreateBy(SessionUtil.getUserId());
        project.setCreateDate(now);
        this.save(project);

        // 添加项目管理员角色 项目成员角色
        SysRole projectRole = roleService.findByRoleName(RoleNameEnum.PROJECT.getName());
        SysRole memberRole = roleService.findByRoleName(RoleNameEnum.MEMBER.getName());

        List<SysUserRole> roles = new ArrayList<>();
        SysUserRole var1 = new SysUserRole();
        var1.setCompanyId(SessionUtil.getCompanyId());
        var1.setProjectId(project.getId());
        var1.setUserId(SessionUtil.getUserId());
        var1.setRoleId(projectRole.getId());
        var1.setCreateBy(SessionUtil.getUserId());
        var1.setCreateDate(now);
        roles.add(var1);

        SysUserRole var2 = new SysUserRole();
        var2.setCompanyId(SessionUtil.getCompanyId());
        var2.setProjectId(project.getId());
        var2.setUserId(SessionUtil.getUserId());
        var2.setRoleId(memberRole.getId());
        var2.setCreateBy(SessionUtil.getUserId());
        var2.setCreateDate(now);
        roles.add(var2);

        userRoleService.saveBatch(roles);

        // 默认给一个团队 -1 代表该用户不属于任何团队
        ProjectTeamMember ptm = new ProjectTeamMember();
        ptm.setCompanyId(SessionUtil.getCompanyId());
        ptm.setProjectId(project.getId());
        ptm.setTeamId(-1L);
        ptm.setMemberBy(SessionUtil.getUserId());
        ptm.setIsManage(0);
        ptm.setCreateBy(SessionUtil.getUserId());
        ptm.setCreateDate(now);
        teamMemberService.save(ptm);
    }

    @Override
    public void update(ProjectUpdateParam param) {

        Long companyId = SessionUtil.getCompanyId();

        Project project = this.baseMapper.findByCompanyAndProjectName(companyId, param.getProjectName(), param.getProjectId());
        if (project != null) {
            throw new IllegalArgumentException("项目名称已存在");
        }

        // 创建项目
        project = new Project();
        project.setId(param.getProjectId());
        project.setAddress(param.getAddress());
        project.setCompanyId(SessionUtil.getCompanyId());
        project.setProjectDesc(param.getProjectDesc());
        project.setPicUrl(param.getPicUrl());
        project.setProjectName(param.getProjectName());
        project.setCoordinate(param.getCoordinate());
        project.setRemarks(param.getRemarks());
        project.setUpdateBy(SessionUtil.getUserId());
        this.updateById(project);
    }

    public void del(Long projectId){
        // 删除项目
        Project project = new Project();
        project.setId(projectId);
        project.setUpdateBy(SessionUtil.getUserId());
        project.setDelFlag(DelFlagEnum.DELETED.getCode());
        this.updateById(project);
    }

    public List<UserProjectDataVO> findByCompany(Long userId){
        return this.baseMapper.findByCompany(SessionUtil.getCompanyId(), userId);
    }

    public Project detail(Long projectId){
        return this.baseMapper.selectById(projectId);
    }
}
